#ifndef AUDIOPLAYER_H
#define AUDIOPLAYER_H

#include <mythtv/audiooutput.h>

#include <qthread.h>
#include <qstring.h>
#include <qapplication.h>
#include <qwaitcondition.h>
#include <qvaluevector.h>

#include "decoder.h"

class SelectionInfo;

class AudioPlayer : public QObject
{
   Q_OBJECT

public:
   class EventInterface
   {
   public:
      typedef enum
      {
         STOP,
         PAUSE,
         PLAY
      } PLAYER_STATE;
      virtual ~EventInterface(){};
      virtual void AudioPlayer_State( SelectionInfo& track, PLAYER_STATE state ) = 0;
      virtual void AudioPlayer_Progress( int position, int length ) = 0;
      virtual void AudioPlayer_Error( QString err ) = 0;
   };

   AudioPlayer( QValueVector<SelectionInfo>& playList );
   void SetHandler( EventInterface* handler );
   void ClearHandler();

   void Play();
   void Suspend();   // Pause
   void Resume();    // Unpause
   void Stop();
   bool Next();
   bool Previous();
   bool Goto( int index );
   void SeekForward();
   void SeekBack();
   void Seek( int pos );

   int Index()
   {
      return CurrentIndex;
   }

   bool IsPlaying()
   {
      return m_IsPlaying;
   }

   void run();

protected:
   void customEvent( QCustomEvent* );

   void PlayFile( const QString& path );
   void openOutputDevice( void );
   void PlaybackStop();
   void stopAll();
   void DecoderStop(void);
   void pause();

   QValueVector<SelectionInfo>& m_PlayList;

private:
   AudioPlayer( const AudioPlayer& );

   int CurrentIndex;
   int CurrentTime;

   bool Active;
   bool m_IsPlaying;
   int currentTime;
   int outputBufferSize;
   int maxTime;

   QWaitCondition m_Wait;
   QString RootPath;
   QIODevice* m_Input;
   AudioOutput* m_Output;
   Decoder* m_Decoder;

   EventInterface* Handler;
   QMutex          Mutex;
};

class AudioPlayerThread : public QThread
{
public:
   AudioPlayerThread( AudioPlayer* player )
   {
      m_Player = player;
   }

protected:
   virtual void run()
   {
      m_Player->run();
   }

private:
   AudioPlayer* m_Player;
};

#endif

